package com.taoyibao.hongcanting.ui.home.delegate;

import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.delegate.BackToolbarDelegate;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.constant.NetCode;
import com.taoyibao.hongcanting.model.BalanceModel;
import com.taoyibao.hongcanting.model.OrderInfoModel;
import com.taoyibao.hongcanting.model.SanFangBaseModel;
import com.taoyibao.hongcanting.ui.home.activity.ChooseCouponActivity;
import com.taoyibao.hongcanting.utils.CodeUtils;
import com.taoyibao.hongcanting.utils.ToastUtils;

import java.util.List;

/**
 * @author TangAnna
 * @description: 支付页面
 * @date :${DATA} 13:44
 */
public class PayDelegate extends BackToolbarDelegate implements View.OnClickListener {

    public double mBalance = 0;//余额
    public double mNeedAmount = 0;//需要支付的金额
    private RadioGroup mRadioGroup;//支付方式

    @Override
    public int getRootLayoutId() {
        return R.layout.activity_pay;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        setTitle("支付");
        setLoadingView();
        setEmptyViewUI(CodeUtils.getIdString(R.string.empty_pay), R.drawable.empty_cart);
//        get(R.id.layout_pay_chooseCoupon).setOnClickListener(this);
        get(R.id.layout_pay_wachart).setOnClickListener(this);
        get(R.id.layout_pay_aliPay).setOnClickListener(this);
        mRadioGroup = get(R.id.rg_pay_way);
    }

    /**
     * 余额UI
     *
     * @param balance
     */
    public void setBalanceUI(List<BalanceModel> balance) {
        if (balance == null || balance.size() < 1) {
            return;
        }
        BalanceModel dataModel = balance.get(0);
        mBalance = dataModel.balance + dataModel.interests;
        ((TextView) get(R.id.tv_pay_balance)).setText(CodeUtils.formatAmountUnit(mBalance));
        setWallet();
    }

    /**
     * 设置订单信息
     */
    public void setOrderInfoUI(SanFangBaseModel<OrderInfoModel> orderInfoModel) {
        if (orderInfoModel.Code == NetCode.HTTP_200 && orderInfoModel.Data != null) {
            OrderInfoModel data = orderInfoModel.Data;
            if (data.order_paystatus == CodeConstant.ORDER_STATUS_NOPAY) {//是未支付订单
                setDataView();
                //订单金额
                mNeedAmount = data.order_totalmoney;
                ((TextView) get(R.id.tv_pay_orderMoney)).setText(CodeUtils.formatAmountUnit(data.order_totalmoney));
                //底部确认支付的按钮文字
                ((TextView) get(R.id.btn_pay_confirm)).setText("确认支付 " + CodeUtils.formatAmountUnit(data.order_totalmoney));
                ((TextView) get(R.id.tv_pay_totalAmount)).setText("总计 " + CodeUtils.formatAmountUnit(data.order_totalmoney));

                //订单号
                if (!TextUtils.isEmpty(data.order_id)) {
                    ((TextView) get(R.id.tv_pay_orderId)).setText(data.order_id);
                }
                //桌号
                if (!TextUtils.isEmpty(data.order_deskid)) {
                    ((TextView) get(R.id.tv_pay_seatId)).setText(data.order_deskid);
                } else {
                    ((TextView) get(R.id.tv_pay_seatId)).setText(CodeUtils.getIdString(R.string.string_waiDai));
                }
                //餐厅名称
                if (!TextUtils.isEmpty(data.order_companyname)) {
                    ((TextView) get(R.id.tv_pay_companyname)).setText(data.order_companyname);
                } else {
                    ((TextView) get(R.id.tv_pay_companyname)).setText(CodeUtils.getIdString(R.string.string_restaurantName));
                }
                setWallet();
            } else {//不是未支付订单
                ToastUtils.showToast("当前订单已被支付");
                setEmptyView();
            }
        } else {
            setErrorView(new Throwable());
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_pay_chooseCoupon://选择优惠券
                ChooseCouponActivity.open(getActivity());
                break;
            case R.id.layout_pay_wallet://钱包支付
                ((RadioButton) get(R.id.rb_pay_wallet)).setChecked(true);
                break;
            case R.id.layout_pay_wachart://微信支付
                ((RadioButton) get(R.id.rb_pay_wechart)).setChecked(true);
                break;
            case R.id.layout_pay_aliPay://支付宝支付
                ((RadioButton) get(R.id.rb_pay_alipay)).setChecked(true);
                break;
        }
    }

    /**
     * 设置钱包支付是否可选
     */
    public void setWallet() {
        if (mBalance > mNeedAmount) {//余额够用 默认使用余额支付
            ((RadioButton) get(R.id.rb_pay_wallet)).setChecked(true);
            ((RadioButton) get(R.id.rb_pay_wallet)).setClickable(true);
            get(R.id.layout_pay_wallet).setOnClickListener(this);
        } else {//余额不够，不可选择
            ((RadioButton) get(R.id.rb_pay_wallet)).setChecked(false);
            ((RadioButton) get(R.id.rb_pay_wallet)).setClickable(false);
            ((RadioButton) get(R.id.rb_pay_alipay)).setChecked(true);//默认选中支付宝支付
            get(R.id.layout_pay_wallet).setOnClickListener(null);
        }
    }

    /**
     * 获取支付方式
     *
     * @return 3 钱包  2 微信  1 支付宝  -1错误
     */
    public int getPayWay() {
        switch (mRadioGroup.getCheckedRadioButtonId()) {
            case R.id.rb_pay_wallet://钱包支付
                return CodeConstant.PAY_WALLET;

            case R.id.rb_pay_wechart://微信支付
                return CodeConstant.PAY_WECHART;

            case R.id.rb_pay_alipay://支付宝支付
                return CodeConstant.PAY_ALIPAY;
        }
        return -1;
    }
}
